﻿/*************************************************************************************
/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       TaskTool
- 机器名称：       YTE
- 命名空间：       LD.Tool.Other
- 文 件 名：       TaskTool
- 创建时间：       2022/6/14 12:03:04
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using FluentFTP.Helpers;
using FluentFTP;
using LD.Host;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System;
using System.Web.Compilation;

namespace LD.Tool
{
    [Tool(FrmParam = "ToolFTPUploadFrm", Text = "FTP上传", ToolType = ToolEnum.Other)]
    public class ToolFTPUpload : BaseTool
    {
        public override OpResult Execute(TB_Tool model)
        {
            stopwatch.Start();

            if (!string.IsNullOrEmpty(model.Param))
            {
                JObject obj = JObject.Parse(model.Param);

                string dir = obj["dir"].ToString();
                string dirNG = obj["dirNG"].ToString();
                string subffix = obj["subffix"].ToString();
                string machineId = (string)Cache.GetCache(CONST.SHARP_MACHINECODE);
                bool isWarSave = (bool)obj["isWarSave"];
                try
                {
                    if (FtpHelper.IsOpen)
                    {
                        var images = Directory.GetFiles(dir, ".", SearchOption.TopDirectoryOnly)
                            .Where(s => s.EndsWith(subffix));
                        if (images.Count() > 0)
                        {
                            bool _isUploadSuccess = true;
                            foreach (var item in images)
                            {
                                string fileName = Path.GetFileName(item);

                                string ftpPath = "/" + machineId + "/" + fileName;
                                FtpStatus ftpStatus = FtpHelper.Client.UploadFile(item, ftpPath, FtpRemoteExists.Overwrite, true);
                                if (ftpStatus != FtpStatus.Success)
                                {
                                    _isUploadSuccess = false;
                                    File.Move(item, dirNG + "/" + fileName);
                                    isSuccess = false;
                                    Log(model, "FTP上传失败:" + fileName, LogLevel.警告);
                                }
                                //不管成功否，都删除
                                File.Delete(item);
                            }
                            isSuccess = _isUploadSuccess;
                        }
                        //else
                        //{
                        //    Log(model, "未发现上传文件", LogLevel.警告);
                        //}
                    }
                    else if (Appbus.StateEnum == StateEnum.手动模式)
                    {
                        isSuccess = false;
                        Log4.Warr("ftp未连接");
                    }
                    else
                        isSuccess = false;

                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    Log4.Error(ex);
                }
            }
            stopwatch.Stop();
            return new OpResult() { IsSuccess = isSuccess, Message = "", ExecuteTime = stopwatch.ElapsedMilliseconds };
        }
    }
}
